package com.foxzzz.greedy;

public class Solution55 {

    public boolean canJump(int[] nums) {
        int dp = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i <= dp) {
                dp = Math.max(dp, i + nums[i]);
            }
        }
        return nums.length <= dp + 1;
    }


    //动规
    public boolean canJump1(int[] nums) {
        if (nums.length == 0) {
            return true;
        }
        boolean dp[] = new boolean[nums.length];
        dp[0] = true;
        for (int i = 0; i < nums.length; i++) {
            if (dp[i] == true) {
                for (int j = i; j < i + nums[i] + 1 && j < nums.length; j++) {
                    dp[j] = true;
                }
            }
        }
        return dp[nums.length - 1];
    }

    public static void main(String[] args) {
        System.out.println(new Solution55().canJump(new int[]{2, 3, 1, 1, 4}));
        System.out.println(new Solution55().canJump(new int[]{3, 2, 1, 0, 4}));
    }
}
